<?php

namespace Account\Controller;

use Common\Controller\ApiController;

class UserCertificationController extends ApiController {

    public function show() {
        $input = $this->request();
        $id = $this->user['id'];
        $users = $this->userinfo;
        if (empty($users['is_cert'])) {
            $this->errorResponse(30007, '不是认证用户，请先实名认证');
        }
        $cert = [];
        $cert = D('UserCertification')->getByUserId($id);
        $cert = transform('UserCertification', 'detail', $cert);
        $this->response($cert);
    }

    public function saveOrUpdate() {
        $input = $this->request();
        $input['user_id'] = $this->user['id'];
        $this->checkNull($input, ['realname', 'id_no']);
        $cert = D('UserCertification')->getByUserId($this->user['id']);
        $user = D('Users')->find($this->user['id']);
        $ucertModel = D('UserCertification');
        if ($cert) {
            $input['status'] = 0;
            $input['reason'] = '';
            $input['update_time'] = time();
            $input = array_merge($cert, $input);
            if ($ucertModel->create($input)) {
                $ucertModel->save();
                $this->modifySuccessResponse();
            } else {
                $this->errorResponse(30007, $ucertModel->getError());
            }
        } else {
            $input['role'] = $user['role'];
            if ($ucertModel->create($input)) {
                $ucertModel->add();
                if ($user['role'] == 3) {
                    $tags = explode(',', $input['tag_ids']);
                    $anchorTags = [];
                    foreach ($tags as $tag) {
                        $anchorTags[] = [
                            'user_id' => $this->user['id'],
                            'tag_id' => $tag,
                        ];
                    }
                    M('anchor_tags')->addAll($anchorTags);
                }
                $this->createdResponse();
            } else {
                $this->errorResponse(30007, $ucertModel->getError());
            }
        }
    }

    /*
     * 聊吧实名认证
     */

    public function store() {
        $input = $this->request();
        $this->checkNull($input, ['realname', 'id_no']);
//        $cert = D('UserCertification')->getByUserId($this->user['id']);
        $user = $this->userinfo;
        if (empty($user['is_cert'])) {
           $idcheck =  new \Util\IdValidator();
           $result = $idcheck->validation_filter_id_card($input['id_no']);
           if(!$result){
               $this->errorResponse('400', '信息填写有误，请检查重新填写');
           }
            //接入第三方聚合数据接口
            $juhe = C('sdk.juhe');
            if ($juhe['is_open']) {
                $realname = urlencode($input['realname']);
                $url = "http://op.juhe.cn/idcard/query?key={$juhe['key_idcard']}&idcard={$input['id_no']}&realname={$realname}";
                $curl = new \Util\Curl();
                $idcard = $curl->get($url);
                $idcard = json_decode($idcard, TRUE);
                if ($idcard['error_code'] > 0) {
                    $this->errorResponse(400, $idcard['reason']);
                }
            }
            $input['user_id'] = $this->user['id'];
            if (D('UserCertification')->add($input)) {
                $user['user_id'] = $this->user['id'];
                $user['is_cert'] = 1; //完成认证
                $user['create_time'] = time(); //完成认证
                D('Users')->save($user);
            }
            $this->createdResponse();
        } else {
            $this->errorResponse(10001, '已是认证用户');
        }
    }

}
